$(function(){

	// alert(1);


function Contr(){
			//取出当前URL
			var ContrStr = window.location.href;
			
			//切字符串
			ContrStr = ContrStr.split('/');
			//alert(ContrStr);
			//取得控制器名
			// alert(ContrStr);
			var arr = '';
			
			for(var i = 0; i < ContrStr.length; i++){
				if(ContrStr[i] == 'index' || ContrStr[i] == 'admin'){
						// alert(i);
						arr = ContrStr[i+1];
						return 	arr;	
				}
			}
			
			
		}
	function getUrl(){

		var ContrStr = window.location.href;
		
		ContrStr = ContrStr.split('/');

		var arr = '';
		for(var i = 0;  i < ContrStr.length; i++ ){
			if(ContrStr[i] == 'index' ){

				arr += ContrStr[i]+'/';
				return arr;
			}
			arr += ContrStr[i]+'/';
		}

	}
  function AjaxSend(Action,controller,cont,str,AjaxUrl){
                 var aa = AjaxUrl + '' + controller +"/"+Action+''+cont;
              alert(aa);

              // alert(11);
        $.ajax({  

            datatype:"json",
                    type : "POST",  //提交方式  
                     // url : "http://localhost/AdminFrame/public/admin/" + controller +"/"+Action+controller,//路径 
                     url : AjaxUrl + '' + controller +"/"+Action+''+cont,//路径 
                     //url : "http://localhost/AdminFrame/public/admin/Bill/withDrawSET",//路径 
                    //url : "http://localhost/AdminFrame/public/admin/user/"+Action+"cate",//路径 
                    data : {  
                        
                        "str":str
                       

                    },//数据，这里使用的是Json格式进行传输  
                    success : function(result) {//返回数据根据结果进行相应的处理
                        
                        alert("修改成功");
                        //转成json 字符串
                        // var ResultJsonObject = $.parseJSON(result);
                        
                        window.location.reload();    
                    },
                    error : function(result){
                        alert("获取失败");
                    } 
                }); 
    }

   
	//首字母转换成大写
	function newUpper(str){
			var NewStr = str.split('');
			var First_Char = NewStr['0'];
			var A_Char = First_Char.toUpperCase();
			for(var i = 0; i < NewStr.length; i++){
				if(i > 0){
					A_Char += NewStr[i]
				}
		
			}
			return A_Char;
		}

	   //取出当前的URL
    function GetUrl(){
        var ContrStr = window.location.href;
        var NewContrStr = ContrStr.split('/');
        //alert(NewContrStr);
        var str = '';
       for(var i = 0; i < NewContrStr.length; i++){

         
         if(NewContrStr[i] == 'index' || NewContrStr[i] == 'admin'){
            str += (NewContrStr[i] + '/');
                return str;
         }
         str += (NewContrStr[i] + '/');
       }
    }

//iCheck插件的全选与全不选
$('#theadcheck').on('ifChecked ifUnchecked', function(event) {
	if (event.type == 'ifChecked') {
		$('input[name = "checkbox1"]').iCheck('check');

	} else {
            // checkboxes.iCheck('uncheck');
            $('input[name = "checkbox1"]').iCheck('uncheck');
            
        }
    });

 //取得check框的那一条内容

	


//动态按钮按下，弹出模态窗口，给出模态窗口内容
$('button[name ="ActionButton"]').click(function(){
	//alert("按下了修改键");
	//读取DIALOG里的input内容	
	//取出当前控制器名称
	
	var controller  = Contr();

	var cont = newUpper(controller);	

	var AjaxUrl = GetUrl();
	
	var InputIndex = ['id','user_id','user_name','all_pay','Business_level','Business_percent','create_time'];

	Action = "modify";
	var str = "";
	
	//alert(aa);

	if(Action == "modify"){
		$(".checked").parent().siblings('td').each(function(){
        		//alert(3);
        	//遍历每个TD的内容
        	str +=	$(this).not(".tdbox").html() +",";

        });
	}


	//末尾的逗号去掉
	if(str.substr(str.length-1) == ','){
		str = str.substr(0,str.length-1);
	}

	var ModifyArray = str.split(',');

	for(var i = 0; i < InputIndex.length; i++){
        	
        	$('#myModal .modal-dialog .modal-body input[name = '+ InputIndex[i]+']').attr('placeholder',ModifyArray[i]);
        }





    
	//调用AJAX 函数发送
	// alert(str);

	
});

//保存按钮按下，取得模态窗口内容，发送AJAX
$('#myModal .modal-footer button[name = "savemodify"]').click(function(){

	var sure = confirm('是否确认修改？')
	if(!sure){
		alert('修改已取消');
		window.location.reload();
		return ;	
	}


	var controller  = Contr();

	var cont = newUpper(controller);	

	var AjaxUrl = GetUrl();
	
	var InputIndex = ['id','user_id','user_name','all_pay','Business_level','Business_percent','create_time'];

	Action = "modify";
	var str = "";

	//取得模态窗口的内容

	for(var i = 0; i < InputIndex.length; i++){
		if(($('#myModal .modal-body input[name ='+ InputIndex[i] +']').prop('value'))){
			//alert("存在");
			str += $('#myModal .modal-body input[name ='+ InputIndex[i] +']').prop('value') + ",";
		}else{
			str += $('#myModal .modal-body input[name ='+ InputIndex[i] +']').attr('placeholder') + ",";
		}

		//str += (taget.attr('value') ? taget.attr('value') : taget.attr('placeholder')) + ",";
	}
	//alert(str);
	//末尾的逗号去掉
	if(str.substr(str.length-1) == ','){
		str = str.substr(0,str.length-1);
	}

	AjaxSend(Action,controller,cont,str,AjaxUrl);
})



//alert(1);




})